// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    recordsList: [],
    activeName: 'team',
    api: 'plugin.team-dividend.api.results.getList',
    is_show: false,
    is_member_show:null,
    is_show_team_buyer:false,

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,

    type:"",
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if(options.type){
      wx.setNavigationBarTitle({
        title: '我的订单',
      })
      this.setData({type:options.type,is_show:false,is_show_team_buyer:false,api:'order.list'});
    }
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {},

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {},

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {},

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {},

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {},

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},

  handleChange(e) {
    this.setData({
      is_member_show:null
    })
    this.data.activeName = e.detail.name;
    if(this.data.activeName === 'people') {
      this.data.api = 'plugin.team-dividend.api.results.getMyOrder';
    }else {
      this.data.api = 'plugin.team-dividend.api.results.getList';
    }
    this.initData();
    this.getData();
  },
  initData() {
    this.setData({
      recordsList: [],
      page: 1,
      total_page: 0,
      isLoadMore: true
    });
  },
  getData() {
    let urlStr = app.getNetAddresss(this.data.api);
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            isLoadMore: true,
          });
          console.log(resData,"resData====")
          if(this.data.type){
            this.setData({
              recordsList: resData.data.data,
              total_page: resData.data.last_page,
            });
          }else{
            if(this.data.activeName === 'people') {
              this.setData({
                recordsList: resData.data.data.data,
                total_page: resData.data.data.last_page,
                is_show_team_buyer: resData.data.is_show_team_buyer
              });
            }else {
              this.setData({
                is_show: resData.data.is_show,
                recordsList: resData.data.data.data,
                total_page: resData.data.data.last_page,
                is_show_team_buyer: resData.data.is_show_team_buyer
              });
            }
          }
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = app.getNetAddresss(this.data.api);
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result == 1) {
            let _data = res.data.data.data;
            if(this.data.type){
              this.setData({
                recordsList: this.data.recordsList.concat(_data),
                isLoadMore: true,
              });
            }else{
              if(this.data.activeName === 'people') {
                this.setData({
                  isLoadMore: true,
                  recordsList: this.data.recordsList.concat(_data.data)
                });
              }else {
                this.setData({
                  isLoadMore: true,
                  recordsList: this.data.recordsList.concat(_data.data)
                });
              }
            }

          } else {
            this.setData({
              page: this.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  memberShow(e){
    if(this.data.is_member_show == e.currentTarget.dataset.name){
      this.setData({
        is_member_show:null
      })
    }else{
      this.setData({
        is_member_show:e.currentTarget.dataset.name
      })
    }
  }
});
